<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Instell extends CI_Controller
{

    /**
     * 安装引导脚本
     */
    public function index()
    {
        $data = '
        <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <META http-equiv="Content-Type" content="text/html; charset=x-user-defined">
    <TITLE>打分系统</TITLE>
    <META name="viewport" content="width=device-width">
    <LINK href="/images/core.css" rel="stylesheet" type="text/css">
    <LINK href="/images/favicon.ico" rel="icon" type="image/x-icon">
    <LINK href="/images/touchicon.png" rel="apple-touch-icon">
</head>
<body class="p-home">
<DIV class="g-hd f-usn">
    <DIV class="g-in">
        <DIV class="m-head">
            <H1><A href="http://xxscore/">打分系统</A></H1></DIV>
    </DIV>
</DIV>
<DIV class="g-bn f-usn">
    <DIV class="g-in">
        <h1>系统安装</h1>
        <p>正在安装数据库：</p>
        ';
        echo $data;
        echo "<p>1.正在创建session表： ...... ";
        $this->_session_table();
        echo "正功<p/>";

        echo "<p>2.正在创建setting表： ...... ";
        $this->_setting_table();
        echo "正功<p/>";

        echo "<p>3.正在创建user表： ...... ";
        $this->_user_table();
        echo "正功<p/>";

        echo "<p>4.正在创建teacher表： ...... ";
        $this->_teacher_table();
        echo "正功<p/>";

        echo "<p>5.正在创建student表： ...... ";
        $this->_student_table();
        echo "正功<p/>";

        echo "<p>6.正在创建relation表： ...... ";
        $this->_relation_table();
        echo "正功<p/>";

        echo "<p>7.正在创建classes表： ...... ";
        $this->_classes_table();
        echo "正功<p/>";

        echo "<p>8.正在创建standard表： ...... ";
        $this->_standard_table();
        echo "正功<p/>";

        echo "<p>9.正在创建score表： ...... ";
        $this->_score_table();
        echo "正功<p/>";

    }

    public function createtable()
    {
        echo $this->db->version();

        $this->_session_table();

        $tables = $this->db->list_tables();

        foreach ($tables as $table) {
            echo $table;
        }
    }

    /**
     * 创建 session表
     */
    private function _session_table()
    {
        $sql = " DROP TABLE IF EXISTS `xs_sessions`; ";
        $this->db->query($sql);

        $sql = "
                CREATE TABLE
                IF NOT EXISTS  `xs_sessions` (
                  session_id varchar(40) DEFAULT '0' NOT NULL,
                  ip_address varchar(45) DEFAULT '0' NOT NULL,
                  user_agent varchar(120) NOT NULL,
                  last_activity int(10) unsigned DEFAULT 0 NOT NULL,
                  user_data text DEFAULT '' NOT NULL,
                  PRIMARY KEY (session_id),
                  KEY `last_activity_idx` (`last_activity`)
                )  COMMENT '系统sexxion表';
        ";
        $ret = $this->db->query($sql);
    }

    /**
     *
     */
    private function _setting_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_setting`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_setting` (
              `setting_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `setting_variable` varchar(32) NOT NULL COMMENT '配制参数名',
              `setting_value` varchar(128) NOT NULL COMMENT '配制参数值',
              `setting_dsc` varchar(1024) NOT NULL COMMENT '配制参数描述',
              `setting_type` TINYINT(1) NOT NULL default '0' COMMENT '0:不显示　1:显示',
              PRIMARY KEY  (`setting_id`),
              UNIQUE KEY `setting_variable` (`setting_variable`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '系统设置表';
        ";
        $ret = $this->db->query($sql);

        $sql = "
        INSERT INTO `xx_setting` (`setting_variable`, `setting_value`, `setting_dsc`, `setting_type`) VALUES
            ('cookies_time', '6000', '登陆有效时间', 1);
        ";
        $ret = $this->db->query($sql);
    }

    /**
     * 用户表
     */
    private function _user_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_user`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_user` (
              `user_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `user_name` varchar(64) NOT NULL COMMENT '用户名',
              `user_pwd` varchar(64) NOT NULL COMMENT '密码',
              `user_role` INT(12) NOT NULL COMMENT '用户角色',
              `user_group` INT(12) NOT NULL COMMENT '用户组 0:系统管理员',
              `add_time` int(10) NULL COMMENT '创建时间',
              `deleteflag` tinyint(1) NOT NULL default '0' COMMENT '是否删除,默认为0 0:未删除 1:已删除',
              PRIMARY KEY  (`user_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '用户表';
        ";
        $ret = $this->db->query($sql);

        $sql = "
        INSERT INTO `xx_user` (`user_name`, `user_pwd`, `user_role`, `user_group`, `add_time`) VALUES
            ('admin', 'admin', 1, 1, " . time() . ");
        ";
        $ret = $this->db->query($sql);
    }

    /**
     * 老师表
     */
    private function _teacher_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_teacher`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_teacher` (
              `teacher_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `teacher_uid` INT(12) NOT NULL COMMENT '对应用户ID',
              `teacher_name` varchar(32) NOT NULL COMMENT '老师姓名',
              `teacher_code` varchar(32) NOT NULL COMMENT '老师工号',
              `add_time` int(10) NULL COMMENT '创建时间',
              `deleteflag` tinyint(1) NOT NULL default '0' COMMENT '是否删除,默认为0 0:未删除 1:已删除',
              PRIMARY KEY  (`teacher_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '老师表';
        ";
        $ret = $this->db->query($sql);
    }

    /**
     * 学生表
     */
    private function _student_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_student`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_student` (
              `student_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `student_uid` INT(12) NOT NULL COMMENT '对应用户ID',
              `student_name` varchar(32) NOT NULL COMMENT '老师姓名',
              `student_code` varchar(32) NOT NULL COMMENT '学号',
              `add_time` int(10) NULL COMMENT '创建时间',
              `deleteflag` tinyint(1) NOT NULL default '0' COMMENT '是否删除,默认为0 0:未删除 1:已删除',
              PRIMARY KEY  (`student_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '学生表';
        ";
        $ret = $this->db->query($sql);
    }

    /**
     * 班级表
     */
    private function _classes_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_classes`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_classes` (
              `classes_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `classes_name` varchar(64) NOT NULL COMMENT '班级名',
              `add_time` int(10) NULL COMMENT '创建时间',
              `deleteflag` tinyint(1) NOT NULL default '0' COMMENT '是否删除,默认为0 0:未删除 1:已删除',
              PRIMARY KEY  (`classes_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '班级表';
        ";
        $ret = $this->db->query($sql);
    }

    /**
     * 班级关系表
     */
    private function _relation_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_relation`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_relation` (
              `relation_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `batch` INT(10) NOT NULL default '1' COMMENT '批次，现在默认都是1',
              `class_id` INT(12) NOT NULL COMMENT '班级ID',
              `bind_id` INT(12) NOT NULL  COMMENT '老师ID 或 学生ID',
              `relation_type` INT(10) NULL DEFAULT 2 COMMENT '关系类型  1：老师－班级 2：学生评班级',
              `relation_exp` varchar(128) NULL DEFAULT '' COMMENT '关系类型说明',
              PRIMARY KEY  (`relation_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '班级关系表';
        ";
        $ret = $this->db->query($sql);
    }

    /**
     * 评分标准表
     */
    private function _standard_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_standard`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_standard` (
              `standard_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `batch` INT(10) NOT NULL default '1' COMMENT '批次，现在默认都是1',
              `standard_title` varchar(32) NOT NULL COMMENT '评分标准 title',
              `standard_value` INT(10) NOT NULL default 0 COMMENT '评分标准计分',
              `standard_limit` INT(10) NOT NULL default 0 COMMENT '评分受限范围,百分比',
              `overflow` INT(12) NOT NULL COMMENT '评分溢出方向',
              PRIMARY KEY  (`standard_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT = 1 COMMENT '评分标准表';
        ";
        $ret = $this->db->query($sql);

        $sql = "
        INSERT INTO `xx_standard` (`standard_id`, `batch`, `standard_title`, `standard_value`, `standard_limit`, `overflow`) VALUES
            (1, '1', 'A', 4, 10, 2),
            (2, '1', 'B', 3, 80, 2),
            (3, '1', 'C', 2, 7, 2),
            (4, '1', 'D', 1, 3, 3);
        ";
        $ret = $this->db->query($sql);
    }

    /**
     * 打分表
     */
    private function _score_table()
    {
        $sql = " DROP TABLE IF EXISTS `xx_score`; ";
        $this->db->query($sql);

        $sql = "
            CREATE TABLE IF NOT EXISTS `xx_score` (
              `score_id` INT (12) UNSIGNED NOT NULL auto_increment,
              `batch` INT(10) NOT NULL default '1' COMMENT '批次，现在默认都是1',
              `standard_id` INT(12) NOT NULL COMMENT '评分ID',
              `original_standard_id` INT(12) NOT NULL COMMENT '原始评分标准ID',
              `candidate` INT(12) NOT NULL  COMMENT '被评分人，一般为老师ID',
              `voter` INT(12) NULL COMMENT '评分人,根据score_type不同，可以为学生或老师',
              `score_type` INT(10) NULL DEFAULT 1 COMMENT '评分类型  1：学生评老师 2：老师评老师',
              PRIMARY KEY  (`score_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '打分表';
        ";
        $ret = $this->db->query($sql);
    }
}
/* 创建数据库 CREATE DATABASE `xxscore` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;*/
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */